<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消"
    wrapClassName="ant-modal-cust-warp"
    style="top:5%;height: 100%;overflow-y: hidden">
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel">
        取消
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item  label="工程名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请输入工程名称" v-decorator.trim="[ 'name', validatorRules.name]"/>
        </a-form-item>
        <a-form-item  label="工程地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请输入工程地址" v-decorator.trim="[ 'unit', validatorRules.unit]"/>
        </a-form-item>
        <a-form-item label="甲方名称" :labelCol="labelCol" :wrapperCol="wrapperCol" >
          <a-input placeholder="请输入甲方名称" v-decorator.trim="[ 'nameA', validatorRules.nameA]" />
        </a-form-item>
         <a-form-item label="纳税人信息" :labelCol="labelCol" :wrapperCol="wrapperCol" >
          <a-input placeholder="请输入纳税人信息" v-decorator.trim="[ 'taxpayerInfo', validatorRules.taxpayerInfo]" />
        </a-form-item>
        <a-form-item label="合同金额" :labelCol="labelCol" :wrapperCol="wrapperCol" >
          <a-input placeholder="请输入合同金额"  v-decorator.trim="[ 'money', validatorRules.money]"  suffix="万元"/>
        </a-form-item>

        <a-form-item label="项目类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select placeholder="请输入项目类型" v-decorator.trim="[ 'type', validatorRules.type]"  :dropdownMatchSelectWidth="false"
           showSearch optionFilterProp="children" @change="changeSelect" >
             <a-select-option  value="双包" >双包</a-select-option>
             <a-select-option  value="供货" >供货</a-select-option>
             <a-select-option  value="清包" >清包</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea :rows="3" placeholder="请输入信息备注" v-decorator="[ 'description' ]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>
<script>
  import pick from 'lodash.pick'
  import Vue from 'vue'
  import JSelectPosition from '@/components/jeecgbiz/JSelectPosition'
  import { ACCESS_TOKEN } from "@/store/mutation-types"
  import {addEngineer,editEngineer,checkEngineer} from '@/api/api'
  import JImageUpload from '../../../components/jeecg/JImageUpload'
  export default {
    name: "EngineerModal",
    components: {
      JImageUpload,
      JSelectPosition
    },
    data () {
      return {
        title:"操作",
        visible: false,
        modalWidth:800,
        drawerWidth:700,
        id:"", //保存工程id
        isReadOnly: false,
        disableSubmit:false,
        dateFormat:"YYYY-MM-DD",
        validatorRules:{
          name:{
            rules: [
              {
              required: true, message: '请输入工程名称!'
            },
            { validator: this.validateName}
            ]
          },
          nameA:{
            rules: [{
              required: true, message: '请输入甲方名称!'
            }]
          },
          unit:{
            rules: [{
              required: true, message: '请输入工程地址!'
            }]
          },
          money:{
            rules: [{
              required: true,type: 'number', message: '请输入正确格式的合同金额(XX或XX.XX)', trigger: 'blur',
               transform (value) {
                  var moneyg = /^\d+$|^\d*\.\d+$/g
                  if (!moneyg.test(value) ) {
                    return false
                  }else{
                    return Number(value)
                  }
                }
            }]
          },
          type:{
            rules: [{
              required: true, message: '请输入项目类型!'
            }]
          }
        },
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        wrapperCol1: {
          xs: { span: 24 },
          sm: { span: 7 },
        },
        uploadLoading:false,
        confirmLoading: false,
        headers:{},
        form:this.$form.createForm(this)
      }
    },
    created () {
      const token = Vue.ls.get(ACCESS_TOKEN);
      this.headers = {"X-Access-Token":token}
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        let that = this;
        that.form.resetFields();
        that.id = record.id;
        that.visible = true;
        that.model = Object.assign({}, record);
        that.$nextTick(() => {
          that.form.setFieldsValue(pick(this.model,'name','unit','nameA','taxpayerInfo','money','type','description'))
        });
      },
      close() {
        this.$emit('close');
        this.visible = false;
        this.disableSubmit = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            let obj;
            if(!this.model.id){
              formData.id = this.id;
              obj=addEngineer(formData);
            }else{
              obj=editEngineer(formData);
            }
            obj.then((res)=>{
              if(res.code === 200){
                that.$emit('ok');
              }else{
                that.$message.warning(res.data.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })

          }
        })
      },
      handleCancel() {
        this.close()
      },

      validateName(rule, value, callback){
        let params = {
          name: value,
          id: this.model.id?this.model.id:0
        };
        checkEngineer(params).then((res)=>{
          if(res && res.code===200) {
            if(!res.data.status){
              callback();
            } else {
              callback("名称已经存在");
            }
          } else {
            callback(res.data);
          }
        });
      }
    }
  }
</script>

<style scoped>

</style>